package com.hrt.datasender.web.dataMaintain;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.btcode.db.result.Row;
import com.btcode.exception.MsgException;
import com.btcode.web.core.annotation.RequestMapping;
import com.btcode.web.core.controller.Controller;
import com.btcode.web.core.unit.RequestUnit;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.hrt.common.web.DSRequestUnit;
import com.hrt.datasender.datadao.model.BcSendPlatformDataStrategyModel;
import com.hrt.datasender.datadao.model.BcSendPlatformModel;
import com.hrt.datasender.datadao.service.BcSendPlatformDataStrategyService;
import com.hrt.datasender.datadao.service.BcSendPlatformService;
import com.hrt.datasender.datastorage.strategy.IStorageStrategy;
import com.hrt.datasender.datastorage.strategy.StrategyLoader;
import com.hrt.datasender.datastorage.strategy.monitordata.MonitorDataStrategy;
import com.hrt.datasender.mq.MqKit;
import com.hrt.datasender.mq.model.PacketModel;
import com.hrt.datasender.process.strategy.IProcessStrategy;
import com.hrt.datasender.process.strategy.ProcessStrategyLoader;

@RequestMapping(path = "DataMaintain")
public class DataMaintainController extends Controller {
    
    @RequestMapping(path = "getModel")
    public void getModel(ServletRequest arg0, ServletResponse arg1) throws Exception {
        BcConfigStorageModel model = DataMaintainService.getInstance().getModel();
        success(model);
    }

    @RequestMapping(path = "saveModel")
    public void saveModel(ServletRequest arg0, ServletResponse arg1) throws Exception {
        HashMap<String, String> params = RequestUnit.getHashMapParmS();
        if(DataMaintainService.getInstance().siteCodeExist(params.get("SITE_CODE"))) {
            throw new MsgException("SITE_CODE参数已存在,请更换");
        }
        BcConfigStorageModel model = DSRequestUnit.getModel(BcConfigStorageModel.class);
        DataMaintainService.getInstance().save(model);
        success(model);
    }

    @RequestMapping(path = "downloadConfig")
    public void downloadConfig(ServletRequest arg0, ServletResponse arg1) throws Exception {
        HashMap<String, String> params = RequestUnit.getHashMapParmS();
        try {
            DataMaintainService.getInstance().downloadConfig(params.get("SITE_CODE"));
        }
        catch(Exception e) {
            throw new MsgException("下载配置错误"+e, e);
        }
        success(null);
    }

    @RequestMapping(path = "uploadConfig")
    public void uploadConfig(ServletRequest arg0, ServletResponse arg1) throws IOException {
        HashMap<String, String> params = RequestUnit.getHashMapParmS();
        try {
            String ret = DataMaintainService.getInstance().uploadConfig(params.get("SITE_CODE"));
            JsonObject result = new JsonParser().parse(ret).getAsJsonObject();
            String resultFlag = result.get("result_flag").getAsString();
            String resultFlagDsc = result.get("result_flag_dsc").getAsString();
            if(!"SUCCESS".equals(resultFlag)) {
                throw new MsgException("上传配置失败"+resultFlagDsc);
            }
        }
        catch(Exception e) {
            throw new MsgException("上传配置错误"+e, e);
        }
        success(null);
    }

    @RequestMapping(path = "readLogData")
    public void readLogData(ServletRequest arg0, ServletResponse arg1) throws IOException {
        HashMap<String, String> params = RequestUnit.getHashMapParmS();
        try {
            DataMaintainService.getInstance().readLogData(params.get("PATH"));
        }
        catch(Exception e) {
            throw new MsgException("读取日志数据错误"+e, e);
        }
        success(null);
    }

}
